package cn.edu.neusoft.yuejian12.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian12.Adapter.QuesAdapter;
import cn.edu.neusoft.yuejian12.JavaBean.Ques;
import cn.edu.neusoft.yuejian12.Model.Model;
import cn.edu.neusoft.yuejian12.R;
import cn.edu.neusoft.yuejian12.Service.MasterService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by 木子李 on 2017/10/25.
 */

public class QuesFragment extends Fragment {
    private Context context;
    private View view;
    private RecyclerView recyc;
    private List<Ques> list;
    private Call<List<Ques>> call;
    private String Id;

    public QuesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_ques, container, false);
        init();
        conn();
        return view;
    }

    private void conn() {
        Retrofit retrofit= Model.getConn();
        MasterService masterService=retrofit.create(MasterService.class);
        call=masterService.getAskList(Id);
        call.enqueue(new Callback<List<Ques>>() {
            @Override
            public void onResponse(Call<List<Ques>> call, Response<List<Ques>> response) {
                list=response.body();
                QuesAdapter a=new QuesAdapter(list,context);
                LinearLayoutManager linear=new LinearLayoutManager(context);
                linear.setOrientation(LinearLayoutManager.VERTICAL);
                recyc.setLayoutManager(linear);
                recyc.setItemAnimator(new DefaultItemAnimator());
                recyc.setAdapter(a);
            }

            @Override
            public void onFailure(Call<List<Ques>> call, Throwable t) {
                Toast.makeText(context,"留言列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void init() {
        context=getActivity();
        Intent intent=getActivity().getIntent();
        Id=intent.getStringExtra("tea_id");
        recyc=(RecyclerView)view.findViewById(R.id.re_ask);
    }

}
